package com.csthink.web.interfaces;

import com.csthink.cache.client.CacheClient;
import com.csthink.cache.constant.RedisCacheKey;
import com.csthink.common.infrastructure.util.IDGenerator;
import com.csthink.mysql.entity.User;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-11
 */
@Slf4j
@Api(tags = "Test 模块")
@RestController
@RequestMapping("/redis")
public class RedisTestController {

    @Resource
    private RedisTemplate<String, User> redisTemplate;

    @Resource
    private CacheClient cacheClient;

    @PostMapping("user-add")
    private User addUser(@RequestBody User user) {
        redisTemplate.opsForValue().set(String.valueOf(user.getId()), user);
        return redisTemplate.opsForValue().get(String.valueOf(user.getId()));
    }

    @GetMapping("find/{id}")
    public User findUserById(@PathVariable("id") String userId) {
        return redisTemplate.opsForValue().get(userId);
    }


    @GetMapping("/test-redis")
    public void testRedis() {
        cacheClient.hSet4HourOrDel("sessionId", "name", "mars");
        cacheClient.hSet4HourOrDel("sessionId", "age", "33");
        String value = cacheClient.hGet("sessionId", "name");
        log.info(value);
    }

    @GetMapping("/del")
    public void del() {
        cacheClient.hDel("sessionId", "name");
        cacheClient.delete(RedisCacheKey.NLP_SESSION_ID);
        cacheClient.hDel(RedisCacheKey.nlpTestBotSession("accountId"), "1");
    }

    @GetMapping("/test-generate-sessionId")
    public void testGenerateSessionId() {
        String nlpSessionId = cacheClient.hGet(RedisCacheKey.nlpTestBotSession("accountId"), "1");
        if (StringUtils.isEmpty(nlpSessionId) || "null".equals(nlpSessionId)) {
            log.warn("nlpSessionId is null. {}", nlpSessionId);

            nlpSessionId = IDGenerator.getId();
            cacheClient.hSet4HourOrDel(
                    RedisCacheKey.nlpTestBotSession("accountId"), "1", nlpSessionId);
            cacheClient.hSet4HourOrDel(
                    RedisCacheKey.nlpTestBotSession("accountId"), "2", nlpSessionId);
            cacheClient.hSet4HourOrDel(
                    RedisCacheKey.nlpTestBotSession("accountId"), "3", nlpSessionId);
        }
        log.info("Bot test,NlpSessionId:{}", nlpSessionId);
    }

    @GetMapping("test-auth")
    public void testAuth() {
        Map<String, String> map =
                cacheClient.hGetAll(RedisCacheKey.eventAuthAccountId("1111", "salesforce"));
        map.put(RedisCacheKey.AUTH_SFDC_ACCESS_TOKEN, "111");
        map.put(RedisCacheKey.AUTH_INSTANCE_URL, "222");
        cacheClient.hSet4Hour(RedisCacheKey.eventAuthAccountId("1111", "salesforce"), map);
        Map map1 = cacheClient.hGetAll(RedisCacheKey.eventAuthAccountId("1111", "salesforce"));
        log.info("map{}", map1);
        cacheClient.expire(RedisCacheKey.eventAuthAccountId("1111", "salesforce"), 60);
    }
}
